package com.travel.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.commons.utils.Constant;
import com.travel.commons.utils.R;
import com.travel.modules.asset.dao.AdvertiseDao;
import com.travel.modules.asset.dao.HotelDao;
import com.travel.modules.asset.dao.RouteDao;
import com.travel.modules.asset.dao.ScenicAreaDao;
import com.travel.modules.asset.entity.Advertise;
import com.travel.modules.asset.entity.Hotel;
import com.travel.modules.asset.entity.Route;
import com.travel.modules.asset.entity.ScenicArea;
import com.travel.modules.sys.dao.SysMsgDao;
import com.travel.modules.sys.entity.SysMsg;
import com.travel.modules.sys.service.SysMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("SysMsgService")
public class SysMsgServiceImpl extends ServiceImpl<SysMsgDao, SysMsg> implements SysMsgService {
    @Resource
    private SysMsgDao sysMsgDao;

    @Resource
    private ScenicAreaDao scenicAreaDao;

    @Resource
    private HotelDao hotelDao;

    @Resource
    private RouteDao routeDao;

    @Resource
    private AdvertiseDao advertiseDao;

    public Object set(String targetId, String table, SysMsg msg, int status) {
        if (Constant.T_SCENIC_AREA.equals(table)) {
            ScenicArea scenicArea = scenicAreaDao.selectOne(new QueryWrapper<ScenicArea>().eq("area_id", targetId));
            if (scenicArea == null) {
                return R.error("审核请求不存在");
            }
            scenicArea.setStatus(status);
            scenicAreaDao.updateById(scenicArea);
        } else if (Constant.T_HOTEL.equals(table)) {
            Hotel hotel = hotelDao.selectOne(new QueryWrapper<Hotel>().eq("hotel_id", targetId));
            if (hotel == null) {
                return R.error("审核请求不存在");
            }
            hotel.setStatus(status);
            hotelDao.updateById(hotel);
        } else if (Constant.T_ROUTE.equals(table)) {
            Route route = routeDao.selectOne(new QueryWrapper<Route>().eq("route_id", targetId));
            if (route == null) {
                return R.error("审核请求不存在");
            }
            route.setStatus(status);
            routeDao.updateById(route);
        } else if (Constant.T_ADVERTISE.equals(table)) {
            Advertise advertise = advertiseDao.selectOne(new QueryWrapper<Advertise>().eq("ad_id", targetId));
            if (advertise == null) {
                return R.error("审核请求不存在");
            }
            advertise.setStatus(status);
            advertiseDao.updateById(advertise);
        }
//        else if (Constant.T_STRATEGY) {
//
//        }
        else {
            return R.error("意外错误");
        }

        msg.setStatus(status);
        sysMsgDao.updateById(msg);
        return null;
    }

    @Override
    public Object pass(String targetId, String table, SysMsg msg) {
        return set(targetId, table, msg, 1);
    }

    @Override
    public Object fail(String targetId, String table, SysMsg msg) {
        return set(targetId, table, msg, -1);
    }

    @Override
    public List<SysMsg> getAllMsg() {
        return sysMsgDao.getAllMsg();
    }
}
